package se.cambio.project.bo.impl;

import org.hibernate.SessionFactory;

import se.cambio.project.bo.DepartmentBo;
import se.cambio.project.dao.DepartmentDao;
import se.cambio.project.model.Department;

public class DepartmentBoImpl implements DepartmentBo{
	
	DepartmentDao departmentDao;
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void setHospitalDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}


	
	public void save(Department department){
		Department department_new = department;
		Department department_old_exist;
		try{
			department_old_exist= departmentDao.findByDepartmentId(department_new.getId());
			
		}catch(NullPointerException e){
			departmentDao.save(department);
		}
		System.out.println("already exist!");
	}
	
	public void update(Department department){
		Department department_new = department;
		
		Department department_old_latest_active = departmentDao.findByDepartmentId(department_new.getId());
		department_old_latest_active.setLatest(0);
		departmentDao.update(department_old_latest_active);
		
		departmentDao.save(department_new);
	}
	
	public void delete(Department department){
		Department department_new = department;
		
		Department department_old_latest_active = departmentDao.findByDepartmentId(department_new.getId());
		department_old_latest_active.setLatest(0);
		departmentDao.update(department_old_latest_active);
		
		department_new.setActive(0);
		departmentDao.save(department_new);
	}
	
	public Department findByDepartmentId(String id){
		return departmentDao.findByDepartmentId(id);
	}
}
